package cn.tank.rabbitwork.service.impl;

import cn.tank.rabbitwork.config.RabbitConfig;
import cn.tank.rabbitwork.entity.Order;
import cn.tank.rabbitwork.entity.OrderStatus;
import cn.tank.rabbitwork.service.IMQService;
import cn.tank.rabbitwork.service.IOrderService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MQService implements IMQService {

    @Autowired
    private RabbitConfig rabbitConfig;

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Autowired
    private IOrderService orderService;

    /**
     * 新订单消息(请求下单)
     * @param order
     */
    @Override
    public void sendNewOrderMsg(Order order) {
        rabbitTemplate.convertAndSend(rabbitConfig.orderEx,rabbitConfig.orderRoutingKey,order);
    }

    /**
     * 下单待支付消息
     * @param order
     */
    @Override
    public void sendOrderSaveMsg(Order order) {
        rabbitTemplate.convertAndSend(rabbitConfig.payWaitingEx,rabbitConfig.payWaitingRoutingKey,order);
    }

    /**
     * 新订单消息
     * @param order
     */
    @RabbitListener(queues = "q.order")
    public void readNewOrderMsg(Order order){
        orderService.addOrder(order);
    }

    @RabbitListener(queues = "q.pay.dead")
    public void readOvertimeMsg(Order order){
        orderService.overtime(order);
    }

}
